package com.lxmes.device.service.impl;

import java.io.IOException;
import java.util.List;

import com.lxmes.common.config.RuoYiConfig;
import com.lxmes.common.core.domain.entity.SysUser;
import com.lxmes.common.core.text.Convert;
import com.lxmes.common.exception.ServiceException;
import com.lxmes.common.utils.DateUtils;
import com.lxmes.common.utils.Sample;
import com.lxmes.common.utils.SecurityUtils;
import com.lxmes.common.utils.StringUtils;
import com.lxmes.device.domain.DeviceFailureRepair;
import com.lxmes.device.domain.DeviceFailureReport;
import com.lxmes.device.domain.NcEmGzbx;
import com.lxmes.device.mapper.NcEmGzbxMapper;
import com.lxmes.device.utils.WaterMarkUtils;
import com.lxmes.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lxmes.device.mapper.NcEmGzwxMapper;
import com.lxmes.device.domain.NcEmGzwx;
import com.lxmes.device.service.INcEmGzwxService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 设备故障维修Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-02-11
 */
@Service
public class NcEmGzwxServiceImpl implements INcEmGzwxService 
{
    @Autowired
    private NcEmGzwxMapper ncEmGzwxMapper;
    @Autowired
    private NcEmGzbxMapper ncEmGzbxMapper;

    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询设备故障维修
     * 
     * @param id 设备故障维修主键
     * @return 设备故障维修
     */
    @Override
    public NcEmGzwx selectNcEmGzwxById(Long id)
    {
        return ncEmGzwxMapper.selectNcEmGzwxById(id);
    }

    /**
     * 查询设备故障维修列表
     * 
     * @param ncEmGzwx 设备故障维修
     * @return 设备故障维修
     */
    @Override
    public List<NcEmGzwx> selectNcEmGzwxList(NcEmGzwx ncEmGzwx)
    {
        return ncEmGzwxMapper.selectNcEmGzwxList(ncEmGzwx);
    }

    /**
     * 新增设备故障维修
     * 
     * @param ncEmGzwx 设备故障维修
     * @return 结果
     */
    @Override
    public int insertNcEmGzwx(NcEmGzwx ncEmGzwx)
    {
        ncEmGzwx.setCreateTime(DateUtils.getNowDate());
        return ncEmGzwxMapper.insertNcEmGzwx(ncEmGzwx);
    }

    /**
     * 修改设备故障维修
     * 
     * @param ncEmGzwx 设备故障维修
     * @return 结果
     */
    @Override
    public int updateNcEmGzwx(NcEmGzwx ncEmGzwx)
    {
        ncEmGzwx.setUpdateTime(DateUtils.getNowDate());
        return ncEmGzwxMapper.updateNcEmGzwx(ncEmGzwx);
    }

    /**
     * 批量删除设备故障维修
     * 
     * @param ids 需要删除的设备故障维修主键
     * @return 结果
     */
    @Override
    public int deleteNcEmGzwxByIds(Long[] ids)
    {
        return ncEmGzwxMapper.deleteNcEmGzwxByIds(ids);
    }

    /**
     * 删除设备故障维修信息
     * 
     * @param id 设备故障维修主键
     * @return 结果
     */
    @Override
    public int deleteNcEmGzwxById(Long id)
    {
        return ncEmGzwxMapper.deleteNcEmGzwxById(id);
    }

    @Override
    @Transactional
    public int cancelReport(String reportId) {
        NcEmGzwx repair =ncEmGzwxMapper.selectByReportId(reportId);
        if(StringUtils.isNull(repair)){
            throw new ServiceException("未找到维修单撤销失败！");
        }
        NcEmGzbx report = ncEmGzbxMapper.selectByReportId(reportId);
        if(report==null||StringUtils.equals(report.getCljd(),"4")){
            throw new ServiceException("此报修单已完成维修不能撤销");
        }
        report.setCljd("0");
        report.setRemark("已撤单-原主修人"+report.getZxr());
        ncEmGzwxMapper.deleteByReportId(reportId);
        return  ncEmGzbxMapper.updateNcEmGzbx(report);
    }

    @Override
    public int updateApiDeviceFailureRepair(NcEmGzwx ncEmGzwx) throws Exception {
        Long majorId= ncEmGzwx.getZxrid();
        Long userId= SecurityUtils.getUserId();
        ncEmGzwx.setJssj(DateUtils.getNowDate());
        if(majorId.longValue()!=userId.longValue()){
            throw new ServiceException("当前用户不是主修人不能提交维修单！");
        }
        if(StringUtils.isNotNull(ncEmGzwx.getWxtp())){
            String path[]= ncEmGzwx.getWxtp().split(",");
            for (int i = 0; i <path.length ; i++) {
                WaterMarkUtils.resizeImage(path[i],360,600);
                WaterMarkUtils.creat(path[i],"主修人："+ncEmGzwx.getZxr(),"维修时间："+ncEmGzwx.getJssj());
                WaterMarkUtils.commPressPicCycle(path[i],0.8);
            }
        }
        if(StringUtils.isNotBlank(ncEmGzwx.getSftj())){
            if(StringUtils.equals("是",ncEmGzwx.getSftj())){
                NcEmGzbx report = ncEmGzbxMapper.selectByReportId(ncEmGzwx.getBxdh());
                String min=DateUtils.getDifferenceMin(ncEmGzwx.getJssj(),report.getFssj());
                ncEmGzwx.setTjsz(min);
            }
        }
        String min=DateUtils.getDifferenceMin(ncEmGzwx.getKssj(),ncEmGzwx.getKssj());
        ncEmGzwx.setWxys(min);
        String reportId=ncEmGzwx.getBxdh();
        NcEmGzbx report = ncEmGzbxMapper.selectByReportId(reportId);
        report.setCljd("3");
        ncEmGzwxMapper.updateNcEmGzwx(ncEmGzwx);
        ncEmGzbxMapper.updateNcEmGzbx(report);
        if(RuoYiConfig.isIsSendSms()) {
            SysUser u = new SysUser();
            u.setDeptId(report.getDeptId());
            List<SysUser> uList = sysUserService.selectUserAdmin(u);
            for (SysUser o : uList) {
                if (StringUtils.isNotBlank(o.getPhonenumber())) {
                    Sample.sendSms(o.getPhonenumber(), reportId, "SMS_249850174");
                }
            }
        }
        return 1;
    }

    @Override
    public NcEmGzwx selectDeviceFailureRepairByReportId(String reportId) {
        return ncEmGzwxMapper.selectByReportId(reportId);
    }
}
